package cn.snailhd.core.base.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.snailhd.core.base.domain.PageVo;

@Component
public class MyBatisDao {
	
	Log log = LogFactory.getLog(this.getClass());
	
	@Autowired
	SqlSessionFactory sqlSessionFactory;
	
	/**
	 *  selectOne 必须返回一个对象或 null 值。
	 *  如果返回值多于一个，那么就会抛出异常。
	 *  如果你不知道返回对象的数量，请使用 selectList
	 * @param statementName
	 * @return
	 */
	public <T> T selectOne(String statementName){
		try(SqlSession session = sqlSessionFactory.openSession() ){
			return session.selectOne(statementName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public <T> T selectOne(String statementName,Object params){
		try(SqlSession session = sqlSessionFactory.openSession() ) {
			return session.selectOne(statementName,params);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public <E> List<E> selectList(String statementName,Object params){
		
		try(SqlSession session = sqlSessionFactory.openSession() ) {
			return session.selectList(statementName,params);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
		
	}
	
	/**
	 * selectMap会将返回的对象的其中一个属性作为 key 值，
	 * 将对象作为 value 值，从而将多结果集转为 Map 类型值
	 * @param statementName
	 * @param params
	 * @param mapKey
	 * @return
	 */
	public <K,V> Map<K,V> selectMap(String statementName,Object params,String mapKey){
		try(SqlSession session = sqlSessionFactory.openSession() ) {
			return session.selectMap(statementName,params, mapKey);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public int insert(String statementName,Object params){
		int re = 0;
		try(SqlSession session = sqlSessionFactory.openSession() ) {
			 re = session.insert(statementName, params);
		} catch (Exception e) {
			log.error(e);
		}
		return re;
	}
	
	
	public int update(String statementName,Object params){
		int re = 0;
		try(SqlSession session = sqlSessionFactory.openSession() ) {
			re = session.update(statementName, params);
		} catch (Exception e) {
			log.error(e);
		}
		return re;
	}
	
	public int delete(String statementName,Object params){
		int re = 0;
		try (SqlSession session = sqlSessionFactory.openSession() ) {
			re = session.delete(statementName, params);
		} catch (Exception e) {
			log.error(e);
		}
		return re;
	}
	
	/**
	 * 简单分页查询
	 * @param statementName
	 * @param params
	 * @return
	 */
	public <T> PageVo<T> selectForPcPage(String statementName,Object params){
		PageVo<T> pv = new PageVo<T>();
		pv.setRows(selectList(statementName, params));
		pv.setCount(selectOne(statementName+"Count", params));
		return pv;
	}
	
	public <T> PageVo<T> selectForMobilePage(String statementName,Object params){
		PageVo<T> pv = new PageVo<T>();
		pv.setRows(selectList(statementName, params));
		return pv;
	}
	
	
	
}
